<template>
  <div id="ratings" class="ratings" ref="ratings">
    <div class="ratings-content">
      <div class="overview">
        <div class="overview-left">
          <span class="score">{{seller.score}}</span>
          <span class="text">综合评分</span>
          <span class="rankRate">高于周边商家{{seller.rankRate}}%</span>
        </div>
        <div class="overview-right">
          <div class="service-wrapper">
            <span class="title">服务态度</span>
            <div class="star-wrapper">
              <v-star :size="36" :score="seller.serviceScore"></v-star>
            </div>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="food-wrapper">
            <span class="title">商品评分</span>
            <div class="star-wrapper">
              <v-star :size="36" :score="seller.foodScore"></v-star>
            </div>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="time-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery-time">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <v-split></v-split>
      <div class="ratingselect-wrapper">
        <v-rating-select @select="selectRating" @toggle="toggleContent" :ratings="ratings" :onlyContent="onlyContent" :selectType="selectType"></v-rating-select>
      </div>
      <div class="rating-list">
        <ul>
          <li v-show="needShow(rating.rateType, rating.text)" class="rating-item border-1px" v-for="(rating, index) in ratings" :key="index">
            <div class="top-wrapper">
              <div class="avatar">
                <img :src="rating.avatar" width="28px" height="28px">
              </div>
              <div class="wrapper">
                <span class="name">{{rating.username}}</span>
                <div class="star-wrapper">
                  <v-star :size="24" :score="rating.score"></v-star>
                </div>
                <span class="delivery-time">{{rating.deliveryTime}}分钟送达</span>
              </div>
              <div class="time">{{rating.rateTime | formatDate}}</div>
            </div>
            <div class="content-wrapper">
              <p class="text">{{rating.text}}</p>
            </div>
            <div v-show="rating.recommend && rating.recommend.length>0" class="recommend">
              <span :class="{'icon-thumb_up': rating.rateType===0, 'icon-thumb_down':rating.rateType===1}"></span>
              <span class="recommend-item" v-for="(item, index) in rating.recommend" :key="index">{{item}}</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';
import vSplit from '../split/split';
import vStar from '../star/star';
import vRatingSelect from '../ratingselect/ratingselect';
import { formatDate } from '../../common/js/data.js';

let ERR_OK = 0;
const POSITIVE = 0;
const NEGATIVE = 1;
const ALL = 2;

export default {
  name: 'ratings',
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      ratings: [],
      selectType: ALL,
      onlyContent: true
    };
  },
  methods: {
    _initScroll() {
      this.scroll = new BScroll(this.$refs.ratings, {
        click: true
      });
    },
    selectRating(type) {
      this.selectType = type;
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },
    toggleContent() {
      this.onlyContent = !this.onlyContent;
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },
    needShow(type, text) {

      if (this.onlyContent && !text) {
        return false;
      }

      if (this.selectType === ALL) {
        return true;
      } else {
        return this.selectType === type;
      }
    }
  },
  created() {
    this.$http.get('/api/ratings').then((response) => {
      if (response.data.errno === ERR_OK) {
        this.ratings = response.data.data;

        //Dom更新，初始化滚动插件
        this.$nextTick(() => {
          this._initScroll();
        });
      }
    });
  },
  filters: {
    formatDate(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }
  },
  components: {
    vSplit,
    vStar,
    vRatingSelect
  }
}
</script>

<style lang="less" scoped>
@import '../../common/styles/mixin.less';

.ratings {
  position: absolute;
  width: 100%;
  top: 174px;
  bottom: 0;
  left: 0;
  overflow: hidden;
  .ratings-content {
    // height: 100%;
    .overview {
      display: flex;
      padding: 18px 0;
      .overview-left {
        flex: 0 0 137px;
        width: 137px;
        text-align: center;
        border-right: 1px solid rgba(7, 17, 27, 0.2);

        @media only screen and (max-width: 320px) {
          flex: 0 0 120px;
          width: 120px;
        }

        .score {
          display: block;
          margin-bottom: 6px;
          font-size: 24px;
          color: rgb(255, 153, 0);
          line-height: 28px;
        }
        .text {
          display: block;
          margin-bottom: 8px;
          font-size: 12px;
          color: rgb(7, 17, 27);
          line-height: 12px;
        }
        .rankRate {
          display: block;
          font-size: 10px;
          color: rgb(7, 17, 27);
          line-height: 10px;
          padding-bottom: 6px;
        }
      }
      .overview-right {
        flex: 1;
        font-size: 0;
        text-align: left;
        margin-left: 24px;
        @media only screen and (max-width: 320px) {
          margin-left: 6px;
        }
        .title {
          display: inline-block;
          vertical-align: top;
          font-size: 12px;
          color: rgb(7, 17, 27);
          line-height: 18px;
        }
        .star-wrapper {
          display: inline-block;
          vertical-align: top;
          margin: 0 12px;
        }
        .score {
          display: inline-block;
          vertical-align: top;
          font-size: 12px;
          color: rgb(255, 153, 0);
          line-height: 18px;
        }
        .service-wrapper {
          font-size: 0;
          margin-bottom: 8px;
        }
        .food-wrapper {
          font-size: 0;
          margin-bottom: 8px;
        }
        .time-wrapper {
          font-size: 0;
          .delivery-time {
            display: inline-block;
            vertical-align: top;
            font-size: 12px;
            line-height: 18px;
            margin-left: 12px;
          }
        }
      }
    }

    .rating-list {
      margin: 0 18px;
      .rating-item {
        .border-1px();
        // margin: 0 18px;
        padding: 18px 0;

        .top-wrapper {
          font-size: 0;
          position: relative;
          margin-bottom: 6px;
          .avatar {
            display: inline-block;
            vertical-align: top;
            margin-right: 12px;
            img {
              border-radius: 14px;
            }
          }
          .wrapper {
            display: inline-block;
            vertical-align: top;
            .name {
              display: block;
              font-size: 10px;
              font-weight: 200;
              color: rgb(147, 153, 159);
              line-height: 12px;
              margin-bottom: 4px;
            }
            .star-wrapper {
              display: inline-block;
              margin-right: 6px;
            }
            .delivery-time {
              font-size: 10px;
              font-weight: 200;
              color: rgb(147, 153, 159);
              line-height: 12px;
            }
          }
          .time {
            position: absolute;
            top: 0;
            right: 0;
            font-size: 10px;
          }
        }

        .content-wrapper {
          margin-left: 40px;
          margin-bottom: 8px;
          font-size: 12px;
          line-height: 18px;
          color: rgb(7, 17, 27);
        }

        .recommend {
          margin-left: 40px;
          font-size: 0;
          .icon-thumb_down,
          .icon-thumb_up {
            font-size: 12px;
            line-height: 16px;
            margin-right: 8px;
            display: inline-block;
            vertical-align: top;
          }

          .icon-thumb_up {
            color: rgb(0, 160, 220);
          }

          .icon-thumb_down {
            color: rgb(103, 187, 191);
          }

          .recommend-item {
            display: inline-block;
            padding: 0 6px;
            border: 1px solid rgba(7, 17, 27, 0.1);
            border-radius: 2px;
            margin-right: 8px;
            margin-bottom: 3px;
            font-size: 9px;
            color: rgb(147, 153, 159);
            line-height: 16px;
          }
        }
      }
    }
  }
}
</style>